package com.atxiaodei.kabudarpc.client;

import com.atxiaodei.kabudarpc.bean.RpcRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @ClassName RemoteInvocationHandler
 * @Deacription TODO
 * @Author 卡布达
 * @Date 2021/10/31 16:49
 * @Version 1.0
 **/
@Component
public class RemoteInvocationHandler implements InvocationHandler {

    @Value("${kabuda.rpc.host}")
    private String host;

    @Value("${kabuda.rpc.port}")
    private int port;

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        // 建立远程链接
        RpcNetTransport rpcNetTransport=new RpcNetTransport(host,port);

        // 设置数据
        RpcRequest rpcRequest=new RpcRequest();
        rpcRequest.setClassName(method.getDeclaringClass().getName());
        rpcRequest.setMethodName(method.getName());
        rpcRequest.setType(method.getParameterTypes());
        rpcRequest.setArgs(args);
        // 传输数据
        Object send = rpcNetTransport.send(rpcRequest);
        return send;
    }
}
